﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace PlanoAulaHelper
{
    public partial class ListDiaNaoUtilForm : FormEdicao
    {
        public ListDiaNaoUtilForm() : base(0)
        {
            InitializeComponent();
        }
        private void CarregarInformacoes()
        {
            if (diaNaoUtilBindingSource.Current != null)
            {
                IdItem = ((DiaNaoUtil)diaNaoUtilBindingSource.Current).Id;

                using (dbContext db = new dbContext())
                {
                    var dia = (from d in db.DiaNaoUtils
                               where d.Id == IdItem
                               select d).Single();
                    edtData.Value = dia.Data;
                    edtDescricao.Text = dia.Descricao;
                }
            }
        }
        private void editarToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CarregarInformacoes();
            HabilitarEdicao(true);
        }
        private void SalvarDiaNaoUtil()
        {
            using (dbContext db = new dbContext())
            {
                var dia = (from d in db.DiaNaoUtils
                           where d.Id == IdItem
                           select d).SingleOrDefault();
                if (dia == null)
                {
                    db.DiaNaoUtils.InsertOnSubmit(new DiaNaoUtil { Data = edtData.Value, Descricao = edtDescricao.Text });
                }
                else
                {
                    dia.Data = edtData.Value;
                    dia.Descricao = edtDescricao.Text;
                }
                db.SubmitChanges();
                HabilitarEdicao(false);

            }
            AtualizarGrid();
        }
        private void MontarCmbAno()
        {
            
            using (dbContext db = new dbContext())
            {
                var anos = (from d in db.DiaNaoUtils
                            select new { Data = new DateTime(d.Data.Year, 01, 01) }).Distinct();
                if( anos.GetEnumerator().MoveNext() )
                    cmbAno.DataSource = anos.ToADOTable(n => new object[] { anos });
            }
        }
        private void MontarCmbMes()
        {
            if (cmbAno.SelectedItem != null)
            {
                using (dbContext db = new dbContext())
                {
                    var mes = (from d in db.DiaNaoUtils
                               where (cmbAno.SelectedItem == null || d.Data.Year == ((DataRowView)cmbAno.SelectedItem)[0].To<DateTime?>().Value.Year)
                               select new { Data = new DateTime(DateTime.Now.Year, d.Data.Month, 01) }).Distinct();
                    if (mes.GetEnumerator().MoveNext())
                        cmbMes.DataSource = mes.ToADOTable(n => new object[] { mes });
                    
                }
            }
        }
        private void AtualizarFiltros()
        {
            MontarCmbAno();
            MontarCmbMes();
            
        }
        private void AtualizarGrid()
        {
            AtualizarGrid(false);
        }
        private void AtualizarGrid(bool atualizarFiltro )
        {
            if( atualizarFiltro )
                AtualizarFiltros();
            using (dbContext db = new dbContext())
            {
                if (cmbMes.SelectedItem != null)
                {
                    diaNaoUtilBindingSource.DataSource = (from d in db.DiaNaoUtils
                                                          where

                                                          (d.Data.Year == ((DataRowView)cmbAno.SelectedItem)[0].To<DateTime>().Year) &&
                                                          (d.Data.Month == ((DataRowView)cmbMes.SelectedItem)[0].To<DateTime>().Month)
                                                          orderby d.Data ascending
                                                          select d);
                }
            }
        }

        private void btnSalvar_Click(object sender, EventArgs e)
        {
            SalvarDiaNaoUtil();
            
        }
        private void HabilitarEdicao(bool Habilitar)
        {
            gbxEdicao.Enabled = Habilitar;
        }
        private void toolStripButtonAdicionar_Click(object sender, EventArgs e)
        {
            this.IdItem = 0;
            edtData.Value = cmbMes.SelectedItem == null ? DateTime.Today : cmbMes.SelectedItem.To<DataRowView>()[0].To<DateTime>().AddDays(DateTime.Today.Day-1);
            edtDescricao.Text = "";
            HabilitarEdicao(true);
        }

        private void apagarToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (diaNaoUtilBindingSource.Current != null && MessageBox.Show("Confirma a exclusão do item?", "Excluir", MessageBoxButtons.OKCancel) == DialogResult.OK)
            {
                IdItem = ((DiaNaoUtil)diaNaoUtilBindingSource.Current).Id;
                using (dbContext db = new dbContext())
                {
                    var dia = (from d in db.DiaNaoUtils
                               where d.Id == IdItem
                               select d).Single();
                    if (dia != null)
                    {
                        db.DiaNaoUtils.DeleteOnSubmit(dia);
                    }
                    db.SubmitChanges();
                }
                AtualizarGrid();
            }
        }

        private void ListDiaNaoUtilForm_Load(object sender, EventArgs e)
        {
            HabilitarEdicao(false);
            AtualizarGrid(true);
        }

        private void cmbAno_Format(object sender, ListControlConvertEventArgs e)
        {
            e.Value = e.ListItem.To<DataRowView>()[0].To<DateTime>().ToString("yyyy");
        }

        private void cmbMes_Format(object sender, ListControlConvertEventArgs e)
        {
            e.Value = e.ListItem.To<DataRowView>()[0].To<DateTime>().ToString("MMMM").ToUpper();

        }

        private void cmbAno_SelectedValueChanged(object sender, EventArgs e)
        {
            MontarCmbMes();
            AtualizarGrid(false);
        }

        private void cmbMes_SelectedValueChanged(object sender, EventArgs e)
        {
            AtualizarGrid(false);

        }

        private void btnAtualizarFiltro_Click(object sender, EventArgs e)
        {
            AtualizarFiltros();
        }

        private void dgvDados_SelectionChanged(object sender, EventArgs e)
        {
            CarregarInformacoes();
            HabilitarEdicao(false);   
        }

        private void dgvDados_DoubleClick(object sender, EventArgs e)
        {
            editarToolStripMenuItem_Click(sender, e);
        }

        private void importarDoGoogleCalendarToolStripMenuItem_Click(object sender, EventArgs e)
        {
            new ImportarFeriadosDoGoogleForm().ShowDialog();
            AtualizarGrid(true);
        }

        private void toolStripButtonVisualizar_Click(object sender, EventArgs e)
        {
            int AnoSelecionado = cmbAno.SelectedItem == null ? 0 : ((DataRowView)cmbAno.SelectedItem)[0].To<DateTime?>().Value.Year;
            new Reports.relDiaNaoUteisForm(AnoSelecionado).ExbirFormulario(this);

        }

        private void apagarTodosDoMesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (cmbAno.SelectedItem != null && cmbMes.SelectedItem != null)
            {
                using (dbContext db = new dbContext())
                {
                    db.Connection.Open();
                    db.Transaction = db.Connection.BeginTransaction();
                    int AnoSelecionado = ((DataRowView)cmbAno.SelectedItem)[0].To<DateTime?>().Value.Year;
                    int MesSelecionado = ((DataRowView)cmbMes.SelectedItem)[0].To<DateTime?>().Value.Month;
                    db.DiaNaoUtils.DeleteAllOnSubmit(from dia in db.DiaNaoUtils
                                                     where dia.Data.Month == MesSelecionado &&
                                                     dia.Data.Year == AnoSelecionado
                                                     select dia
                        );
                    db.SubmitChanges();
                    db.Transaction.Commit();
                }
                AtualizarGrid(true);
            }
            else
            {
                MessageBox.Show("Informe um mês/ano a ser excluído.");
            }
        }

        private void apagarTodosDoAnoToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (cmbAno.SelectedItem != null)
            {
                using (dbContext db = new dbContext())
                {
                    db.Connection.Open();
                    db.Transaction = db.Connection.BeginTransaction();
                    int AnoSelecionado = ((DataRowView)cmbAno.SelectedItem)[0].To<DateTime?>().Value.Year;
                    db.DiaNaoUtils.DeleteAllOnSubmit(from dia in db.DiaNaoUtils
                                                     where dia.Data.Year == AnoSelecionado
                                                     select dia
                        );
                    db.SubmitChanges();
                    db.Transaction.Commit();
                }
                AtualizarGrid(true);
            }
            else
            {
                MessageBox.Show("Informe um ano a ser excluído.");
            }
        }

        private void copiarFeriadosParaOProximoAnoToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (cmbAno.SelectedItem != null)
            {
                using (dbContext db = new dbContext())
                {
                    db.Connection.Open();
                    db.Transaction = db.Connection.BeginTransaction();
                    int AnoSelecionado = ((DataRowView)cmbAno.SelectedItem)[0].To<DateTime?>().Value.Year;
                    var dias = from dia in db.DiaNaoUtils
                               where dia.Data.Year == AnoSelecionado
                               select dia;
                    foreach (DiaNaoUtil dia in dias)
                    {
                        db.DiaNaoUtils.InsertOnSubmit(new DiaNaoUtil { Data = dia.Data.AddYears(1), Descricao = dia.Descricao });
                    }
                    db.SubmitChanges();
                    db.Transaction.Commit();
                }
                AtualizarGrid(true);
            }
            else
            {
                MessageBox.Show("Informe um ano a ser excluído.");
            }
        }

       
    }
}
